package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.business.entity.YytBaseCatalog;
import com.ruoyi.common.business.entity.YytHomeAdv;
import com.ruoyi.common.business.entity.YytHomeSubject;
import com.ruoyi.common.business.service.YytBaseCatalogService;
import com.ruoyi.common.business.service.YytHomeAdvService;
import com.ruoyi.common.business.service.YytHomeSubjectService;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.pms.service.YytPmsSkuInfoService;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import com.ruoyi.vo.SwiperSkuInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 首页相关接口
 * 注意此处接口已添加不需要token验证，如有需要添加验证的需手动添加
 *
 * @author tanxingxian
 * @date 2021-12-20 17:32
 */
@RestController
@RequestMapping("/mainHome")
public class MainHomeController {

    @Autowired
    private YytHomeAdvService yytHomeAdvService;
    @Autowired
    private YytHomeSubjectService yytHomeSubjectService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private YytPmsSkuInfoService yytPmsSkuInfoService;
    @Autowired
    private YytBaseCatalogService yytBaseCatalogService;


    /**
     * 获取轮播图列表
     */
//    @GetMapping("/getSwiperList")
    public ResponseResult getSwiperList() {
        return ResponseResult.success(yytHomeAdvService.clientSwiperList());
    }

    /**
     * 新版获取轮播图列表
     * 随机获取几条商品放到轮播图板块
     * @return
     */
    @GetMapping("/getSwiperList")
    public ResponseResult newGetSwiperList(){
        List<SwiperSkuInfoVo> result = new ArrayList<>();
        List<YytPmsSkuInfo> list = yytPmsSkuInfoService.getSwiperList();
        Set<Integer> set = new HashSet<>();
        Random random = new Random();
        while (set.size() < 5){
            set.add(random.nextInt(list.size()));
        }
        System.out.println(set);
        List<Integer> list1 = new ArrayList<>(set);
        for (int i = 0; i < list1.size(); i++){
            SwiperSkuInfoVo swiperSkuInfoVo = new SwiperSkuInfoVo();
            BeanUtils.copyProperties(list.get(list1.get(i)),swiperSkuInfoVo);
            swiperSkuInfoVo.setImage(list.get(list1.get(i)).getSkuDefaultImg());
            result.add(swiperSkuInfoVo);
        }
        return ResponseResult.success(result);
    }


    /**
     * 轮播图添加点击数
     */
    @PostMapping("/addSwiperCount")
    public ResponseResult addSwiperCount(@RequestBody YytHomeAdv adv) {
        yytHomeAdvService.increaseClickCount(adv.getId());
        return ResponseResult.success();
    }

    /**
     * 获取专题列表
     */
    @GetMapping("/getSubjectList")
    public ResponseResult getSubjectList() {
        List<YytHomeSubject> list = yytHomeSubjectService.list(new LambdaQueryWrapper<YytHomeSubject>()
                .eq(YytHomeSubject::getStatus, 0).orderByAsc(YytHomeSubject::getSort));
        List<List<YytHomeSubject>> result = new ArrayList<>(list.size() / 10 + 1);
        List<YytHomeSubject> itemList = new ArrayList<>(10);
        // 组装成列表嵌套列表 内列表10个一组
        for (YytHomeSubject item : list) {
            if (itemList.size() >= 10) {
                result.add(itemList);
                itemList = new ArrayList<>(10);
            }
            itemList.add(item);
        }
        result.add(itemList);
        return ResponseResult.success(result);
    }

    /**
     * 获取商品第一层分类
     */
    @GetMapping("/getCommodityClass")
    public ResponseResult getCommodityClass(@RequestParam("catalogType") Integer catalogType) {
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0).eq("status", 0).eq("del_flag", 0).eq("catalog_type", catalogType);
        List<YytBaseCatalog> list = yytBaseCatalogService.list(wrapper);
        List<List<YytBaseCatalog>> result = new ArrayList<>(list.size() / 10 + 1);
        List<YytBaseCatalog> itemList = new ArrayList<>(10);
        for (YytBaseCatalog item : list) {
            if (itemList.size() >= 10) {
                result.add(itemList);
                itemList = new ArrayList<>(10);
            }
            itemList.add(item);
        }
        result.add(itemList);
        return ResponseResult.success(result);
    }

}
